#include "stm32f10x.h"                  // Device header

void AD_Init(int is_continuous)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	RCC_ADCCLKConfig(RCC_PCLK2_Div6); //分频 ADCCLK = 72Mhz/6=12Mhz
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//配置adc1，选定通道4，规则组的首位，采样时间为55个周期
	ADC_RegularChannelConfig(ADC1,ADC_Channel_4,1,ADC_SampleTime_55Cycles5);
	
	ADC_InitTypeDef ADC_InitStructure;
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式以及双AD模式
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //触发源选择软件触发
	ADC_InitStructure.ADC_ContinuousConvMode = is_continuous ? ENABLE : DISABLE; //单次转换
	ADC_InitStructure.ADC_ScanConvMode = DISABLE; //非扫描模式
	ADC_InitStructure.ADC_NbrOfChannel = 1;
	ADC_Init(ADC1,&ADC_InitStructure);
	 
	ADC_Cmd(ADC1,ENABLE);
	
	ADC_ResetCalibration(ADC1); //进行校准
	while(ADC_GetResetCalibrationStatus(ADC1)/* == SET*/);
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));
	
	if(is_continuous) ADC_SoftwareStartConvCmd(ADC1,ENABLE); //连续模式只需要触发一次
}

void AD_WithDMA_Init(u32 destAddr)
{
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
	
	RCC_ADCCLKConfig(RCC_PCLK2_Div6); //分频 ADCCLK = 72Mhz/6=12Mhz
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入模式
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//配置adc1，选定通道4，规则组的首位，采样时间为55个周期
	ADC_RegularChannelConfig(ADC1,ADC_Channel_3,1,ADC_SampleTime_55Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_4,2,ADC_SampleTime_55Cycles5);
	
	ADC_InitTypeDef ADC_InitStructure;
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式以及双AD模式
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //触发源选择软件触发
	ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //单次转换
	ADC_InitStructure.ADC_ScanConvMode = ENABLE; //扫描模式
	ADC_InitStructure.ADC_NbrOfChannel = 2;
	ADC_Init(ADC1,&ADC_InitStructure);
	
	DMA_InitTypeDef DMA_InitStructure;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//Byte,Half_Word(16bit),Word(32bit)
	DMA_InitStructure.DMA_PeripheralInc  = DMA_PeripheralInc_Disable;//不自增
	DMA_InitStructure.DMA_MemoryBaseAddr = destAddr;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure.DMA_MemoryInc  = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向：外设作为传输起点
	DMA_InitStructure.DMA_BufferSize = 2;//传输次数
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//还有Circular为自动重装模式
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//内存到内存（实则为开启软件触发）（disable为硬件触发
	DMA_InitStructure.DMA_Priority =  DMA_Priority_Medium;//有多个通道时可以控制通道优先级
	DMA_Init(DMA1_Channel1,&DMA_InitStructure);
	
	DMA_Cmd(DMA1_Channel1,ENABLE);
	ADC_DMACmd(ADC1,ENABLE);//开启DMA的Adc触发源
	ADC_Cmd(ADC1,ENABLE);
	
	ADC_ResetCalibration(ADC1); //进行校准
	while(ADC_GetResetCalibrationStatus(ADC1)/* == SET*/);
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));

}

void AD_DMA_TransferValue(void)
{
	DMA_Cmd(DMA1_Channel1,DISABLE);
	DMA_SetCurrDataCounter(DMA1_Channel1,2);
	DMA_Cmd(DMA1_Channel1,ENABLE);
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);//等待传输完成
	DMA_ClearFlag(DMA1_FLAG_TC1);
}

u16 AD_GetValue(void)
{
	return ADC_GetConversionValue(ADC1);
}

u16 AD_StartAndGetValue(uint8_t ADC_Channel)
{
	ADC_RegularChannelConfig(ADC1,ADC_Channel,1,ADC_SampleTime_55Cycles5);
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	//等待周期55+12.5 = 68约为5.6us
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);
	return ADC_GetConversionValue(ADC1); //读取时会自动清除EOC标志位
}


